package equals.Demo;

import java.util.Objects;

class Stuedent{
    private String name;
    private int age;

    public Stuedent(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        //自己和自己比较
        if (this == o) return true;
        //如果 o 是null对象，或者
        // 虽然可以使用instanceof关键字，但getClass()可以更精确地获取对象的实际运行时类。
        if (o == null || getClass() != o.getClass()) return false;
        Stuedent stuedent = (Stuedent) o;
        return age == stuedent.age && Objects.equals(name, stuedent.name);
    }
}

public class Test {

    public static void main(String[] args) {
        Stuedent stuedent1 = new Stuedent("lisi",11);
        Stuedent stuedent2 = new Stuedent("lisi",11);
        System.out.println(stuedent1.equals(stuedent2));
    }
}
